package com.alibaba.csp.sentinel.dashboard.rule.nacos.gateway;

import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.AbstractRuleNacosProvider;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelNacosConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author marcher
 */
@Component("gatewayFlowRuleNacosProvider")
public class GatewayFlowRuleNacosProvider extends AbstractRuleNacosProvider<List<GatewayFlowRuleEntity>> {

    @Override
    public List<GatewayFlowRuleEntity> getRules(String appName,String ip,Integer port) throws Exception {
        List<GatewayFlowRule> rules = getOriginRules(appName, GatewayFlowRule.class);
        if (CollectionUtils.isEmpty(rules)) {
            return new ArrayList<>();
        }

        return rules.stream().map(rule -> GatewayFlowRuleEntity.fromGatewayFlowRule(appName, ip, port, rule)).collect(Collectors.toList());
    }

    @Override
    protected String postfix() {
        return SentinelNacosConstants.GATEWAY_FLOW_DATA_ID_POSTFIX;
    }
}